package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * SpringMVC对于参数的接受类型：
     * 默认：QueryString(@RequestParam(xxxx))
     * RequestBody:Body(@RequestBody--将请求体中的json数据转为实体类对象数据)
     * <p>
     * 前提：封装类型中的属性必须要和前端传入的key值保持一致
     *
     * @param params
     * @param model
     * @return
     */
    @PostMapping("course/list")
//    @Override
    public PageVO queryCourseBaseList(PageRequestParams params,
                                      @RequestBody QueryCourseBaseModel model) {
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();

        PageVO<CourseBaseDTO> pageVo = courseBaseService.queryCourseBaseList(params, model, companyId);
        return pageVo;
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //把接收到的vo数据转换成dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);

        //通过工具类获得公司的companyId
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseDTO.setCompanyId(companyId);

        return courseBaseService.createCourseBase(courseBaseDTO);
    }

    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getById(@PathVariable Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        return courseBaseService.getByCourseId(courseBaseId,companyId);
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO vo) {

        // 1.将vo数据转为dto数据
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);

        // 2.获得教学机构的id值
        Long companyId = SecurityUtil.getCompanyId();

        dto.setCompanyId(companyId);

        // 3.调用service层获得结果数据
        CourseBaseDTO result = courseBaseService.modifyCourseBase(dto);

        return result;
    }

    @DeleteMapping("course/{courseBaseId}")
    public void removeCoursebase(@PathVariable Long courseBaseId) {
        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }

    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }
}
